package cn.ante.ymcc.service.impl;

import cn.ante.ymcc.service.ICreateTokenService;
import cn.ante.ymcc.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CreateTokenServiceImpl implements ICreateTokenService {
    // 定义一个redis中key
    private static final String TOKEN_KEY = "token:%s:%s";

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public String getToken(String courseId) {
        // 生成唯一得token值
        String token = StrUtils.getComplexRandomString(6);

        // 将生成得token存入redis中,key使用登录id+课程id进行拼接
        Long loginId = 3L;
        String key = String.format(TOKEN_KEY,loginId,courseId);
        redisTemplate.opsForValue().set(key,token,10, TimeUnit.MINUTES);
        return token;
    }
}
